// Copyright 2022 a.o.smith ALL Rights Reserved
package com.liuxinlong.modules.sync.service.impl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.liuxinlong.constant.TimeFormatConstants;
import com.liuxinlong.modules.material.dto.MaterialInfoDto;
import com.liuxinlong.utils.ObjectUtils;
import com.liuxinlong.utils.TimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.liuxinlong.annotation.DataSource;
import com.liuxinlong.enums.DataSourceEnum;
import com.liuxinlong.modules.mapper.qad.PTMstrMapper;
import com.liuxinlong.modules.sync.service.SyncService;

import lombok.extern.slf4j.Slf4j;

/**
 * 同步服务service实现
 *
 * @author liuxinlong@hotwater.com.cn
 * @version 1.0.0
 * @since 2022年2月22日
 */
@Service
@Slf4j
public class SyncServiceImpl implements SyncService {

	@Autowired
	private PTMstrMapper ptMstrMapper;

	@DataSource(DataSourceEnum.PGS)
	@Override
	public List<Map<String, Object>> syncMaterial(Map<String, Object> queryParam) {
		int startNum = (int) queryParam.get("startNum");
		List<Map<String, Object>> originList = ptMstrMapper.pagePTMstrList(queryParam);
		List<Map<String, Object>> resultList = new ArrayList<>(originList.size());
		int sort = startNum + 1;
		for (Map<String, Object> item : originList) {
			Timestamp modTimeStr = (Timestamp) item.get("pt_mod_date");
			if (ObjectUtils.isEmpty(modTimeStr)) {
				item.put("pt_mod_date", "");
			} else {
				item.put("pt_mod_date", TimeUtils.formatTime(modTimeStr.toString(), TimeFormatConstants.YYYY_MM_DD_HH_MM_SS_sss, TimeFormatConstants.YYYYMMDD));
			}
			Timestamp addTimeStr = (Timestamp) item.get("pt_added");
			if (ObjectUtils.isEmpty(addTimeStr)) {
				item.put("pt_added", "");
			} else {
				item.put("pt_added", TimeUtils.formatTime(addTimeStr.toString(), TimeFormatConstants.YYYY_MM_DD_HH_MM_SS_sss, TimeFormatConstants.YYYYMMDD));
			}
			item.put("sort", sort);
			resultList.add(item);
			sort++;
		}
		return resultList;
	}

	@DataSource(DataSourceEnum.PGS)
	@Override
	public int countMaterial(Map<String, Object> queryParam) {
		return ptMstrMapper.getMaterialCount(queryParam);
	}

	@DataSource(DataSourceEnum.PGS)
	@Override
	public Map<String, Object> getOneMaterial(String materialNo, String region) {
		return ptMstrMapper.getOneMaterial(materialNo,region);
	}

	@DataSource(DataSourceEnum.PGS)
	@Override
	public Map<String, Object> getOneSiteMaterial(String materialNo, String region) {
		return ptMstrMapper.getOneSiteMaterial(materialNo,region);
	}

	@DataSource(DataSourceEnum.PGS)
	@Override
	public Map<String, Map<String, Object>> getMaterials(List<String> materialNoList, String region) {
		return ptMstrMapper.getMaterials(materialNoList,region);
	}

	@DataSource(DataSourceEnum.QAD)
	@Override
	public List<MaterialInfoDto> getChildMaterials(List<String> materialNoList, String region,String startTime) {
		return ptMstrMapper.getChildMaterials(materialNoList,region,startTime);
	}

	@DataSource(DataSourceEnum.PGS)
	@Override
	public List<Map<String, String>> asyncQueryMaterial(String keyword, String region) {
		return ptMstrMapper.asyncQueryMaterial(keyword,region);
	}

	@DataSource(DataSourceEnum.PGS)
	@Override
	public List<Map<String, String>> asyncQuerySiteMaterial(String keyword, String region) {
		return ptMstrMapper.asyncQuerySiteMaterial(keyword,region);
	}
	@DataSource(DataSourceEnum.QAD)
	@Override
	public List<MaterialInfoDto> getParentMaterials(List<String> materialNoList, String region) {
		return ptMstrMapper.getParentMaterials(materialNoList,region);
	}

	@DataSource(DataSourceEnum.QAD)
	@Override
	public List<Map<String,Object>> getMaterialInfo(String materialNo) {
		return ptMstrMapper.getMaterialInfo(materialNo);
	}

	@DataSource(DataSourceEnum.PGS)
	@Override
	public List<Map<String, Object>> getPgsMaterialInfo(String materialNo) {
		return ptMstrMapper.getPgsMaterialInfo(materialNo);
	}

	@DataSource(DataSourceEnum.QAD)
	@Override
	public Map<String, Object> getMaterialExpInfo(String materialNo, String region) {
		return ptMstrMapper.getMaterialExpandInfo(materialNo, region);
	}

	@DataSource(DataSourceEnum.PGS)
	@Override
	public List<Map<String, Object>> getPgsSiteMaterialInfo(String materialNo) {
		return ptMstrMapper.getPgsSiteMaterialInfo(materialNo);
	}
}
